//
//  MainViewController.m
//  BosidengIphone
//
//  Created by  on 11-12-25.
//  Copyright (c) 2011年 __MyCompanyName__. All rights reserved.
//

#import "MainViewController.h"
#import "NewsViewController.h"
#import "ProductViewController.h"
#import "MyShowIndexViewController.h"
#import "VideoListViewController.h"
#import "StoreViewController.h"
#import "SuggestViewController.h"
#import "AboutViewController.h"
#import "TopMenuViewController.h"
#import "BrandSelectViewController.h"
#import "AppDelegate.h"
#import "ProductData.h"

@implementation MainViewController
@synthesize topMenuViewController;
@synthesize contentView;
@synthesize titleLabel;
@synthesize backView;
@synthesize headView;
@synthesize contentViewController;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        naviViewControllers = [[NSMutableArray alloc] init];
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

- (void)dealloc
{
    [contentViewController release];
    [headView release];
    [backView release];
    [naviViewControllers release];
    [contentView release];
    [topMenuViewController release];
    [titleLabel release];
    [super dealloc];
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    backView.hidden = YES;
    TopMenuViewController *viewController = [[TopMenuViewController alloc] initWithNibName:@"TopMenuViewController" bundle:nil];
    viewController.delegate = self;
    viewController.topMenus = [NSArray arrayWithObjects:
                               [AppDelegate getLanguageString:@"news_label_title"],
                               [AppDelegate getLanguageString:@"product_label_title"],
                               [AppDelegate getLanguageString:@"show_label_title"],
                               [AppDelegate getLanguageString:@"video_label_title"],
                               [AppDelegate getLanguageString:@"store_label_title"],
                               [AppDelegate getLanguageString:@"suggest_label_title"],
                               [AppDelegate getLanguageString:@"about_label_title"],
                               nil];
    self.topMenuViewController = viewController;
    [viewController release];
    
    UIView *topMenuView = topMenuViewController.view;
    topMenuView.frame = CGRectMake(0, -460, 320, 460);
    [self.view addSubview:topMenuView];
    if (contentViewController) {
        [self changeTopViewController:contentViewController];
    }
    /*
    IndexViewController *pview = [[IndexViewController alloc] initWithNibName:@"IndexViewController" bundle:nil];
    pview.products = [ProductData getRecommendItems];
    [self changeTopViewController:pview];
    [pview release];
    */
}

- (void)viewDidUnload
{
    [super viewDidUnload];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - top menu click

- (void)showMenuAnimation:(UIView*)view {
	[UIView beginAnimations:nil context:nil];
	[UIView setAnimationDuration:0.5];
	[UIView setAnimationDelay:0.0];
    CGAffineTransform transform = CGAffineTransformMakeTranslation(0, 460);
    view.transform = transform;
	[UIView commitAnimations];
}

- (void)rotateViewLandscapeLeft {
    CGAffineTransform transform = self.view.transform;
    
    // use the status bar frame to determine the center point of the window's content area.
    CGRect statusBarFrame = [[UIApplication sharedApplication] statusBarFrame];
    CGRect bounds = CGRectMake(0, 0, statusBarFrame.size.height, statusBarFrame.origin.x);
    CGPoint center = CGPointMake(bounds.size.height / 2.0, bounds.size.width / 2.0);
    // set the center point of the view to the center point of the window's content area.
    self.view.center = center;
    
    // Rotate the view 90 degrees around its new center point. 
    transform = CGAffineTransformRotate(transform, (M_PI / 2.0));
    self.view.transform = transform;
}

- (void)hiddenHeadView {
    headView.hidden = YES;
}

- (void)showHeadView {
    headView.hidden = NO;
}

- (IBAction)showTopMenu:(id)sender {
    [self showMenuAnimation:topMenuViewController.view];
}

- (IBAction)backView:(id)sender {
    [self popViewController];
}

- (void)popViewController {
    int viewCount = [naviViewControllers count];
    if (viewCount == 0) {
        return;
    }
    int index = viewCount - 1;
    UIViewController *viewController = [naviViewControllers objectAtIndex:index];
    [viewController viewWillDisappear:NO];
    [viewController.view removeFromSuperview];
    [viewController viewDidDisappear:NO];
    [naviViewControllers removeObject:viewController];
    
    if ([naviViewControllers count] == 1) {
        backView.hidden = YES;
    } else {
        backView.hidden = NO;
    }
}

- (void)pushViewController:(UIViewController *)viewController {
    [naviViewControllers addObject:viewController];
    if ([viewController respondsToSelector:@selector(setMainViewController:)]) {
        [viewController setMainViewController:self];
    }
    
    CGRect frame = CGRectMake(0, 0, 320, 416);
	//NSLog(@"frame(%.0f, %.0f, %.0f, %.0f)", frame.origin.x, frame.origin.y, frame.size.width, frame.size.height);
	viewController.view.frame = frame;
	[viewController viewWillAppear:NO];
	[contentView addSubview:viewController.view];
	[viewController viewDidAppear:NO];
    
    if ([naviViewControllers count] > 1) {
        backView.hidden = NO;
    } else {
        backView.hidden = YES;
    }
}

- (UIViewController *)currentViewController {
    int viewCount = [naviViewControllers count];
    if (viewCount == 0) {
        return nil;
    }
    int index = viewCount - 1;
    UIViewController *viewController = [naviViewControllers objectAtIndex:index];
    return viewController;
}

- (void)changeTopViewController:(UIViewController *)viewController {
    int viewCount = [naviViewControllers count];
    for (int i = viewCount - 1; i >= 0; i--) {
        UIViewController *viewController = [naviViewControllers objectAtIndex:i];
        [viewController viewWillDisappear:NO];
        [viewController.view removeFromSuperview];
        [viewController viewDidDisappear:NO];
        [naviViewControllers removeObject:viewController];
    }
    [self pushViewController:viewController];
}

- (void)changeTitle:(NSString *)title {
    titleLabel.text = title;
}

- (void)changeViewController:(NSInteger)index andTitle:(NSString *)title {
    if (index == 0) {
        NewsViewController *viewController = [[NewsViewController alloc] initWithNibName:@"NewsViewController" bundle:nil];
        [self changeTopViewController:viewController];
        [viewController release];
    } else if (index == 1) {
        ProductViewController *viewController = [[ProductViewController alloc] initWithNibName:@"ProductViewController" bundle:nil];
        viewController.products = [ProductData getAllItems];
        [self changeTopViewController:viewController];
        [viewController release];
    } else if (index == 2) {
        MyShowIndexViewController *viewController = [[MyShowIndexViewController alloc] initWithNibName:@"MyShowIndexViewController" bundle:nil];
        [self changeTopViewController:viewController];
        [viewController release];
    } else if (index == 3) {
        VideoListViewController *viewController = [[VideoListViewController alloc] initWithNibName:@"VideoListViewController" bundle:nil];
        [self changeTopViewController:viewController];
        [viewController release];
    } else if (index == 4) {
        StoreViewController *viewController = [[StoreViewController alloc] initWithNibName:@"StoreViewController" bundle:nil];
        [self changeTopViewController:viewController];
        [viewController release];
    } else if (index == 5) {
        SuggestViewController *viewController = [[SuggestViewController alloc] initWithNibName:@"SuggestViewController" bundle:nil];
        [self changeTopViewController:viewController];
        [viewController release];
    } else if (index == 6) {
        AboutViewController *viewController = [[AboutViewController alloc] initWithNibName:@"AboutViewController" bundle:nil];
        viewController.mData = [BrandData getFirstItem];
        [self changeTopViewController:viewController];
        [viewController release];
    }
    [self changeTitle:title];
}

@end
